package com.zhhs.project.act.activity.controller;


import com.zhhs.common.exception.file.InvalidExtensionException;
import com.zhhs.common.utils.poi.ExcelUtil;
import com.zhhs.framework.aspectj.lang.annotation.Log;
import com.zhhs.framework.aspectj.lang.enums.BusinessType;
import com.zhhs.framework.web.controller.BaseController;
import com.zhhs.framework.web.domain.AjaxResult;
import com.zhhs.framework.web.page.TableDataInfo;
import com.zhhs.project.system.service.ISysConfigService;
import com.zhhs.project.act.activity.domian.ActActivity;
import com.zhhs.project.act.activity.domian.ActActivityMarket;
import com.zhhs.project.act.activity.domian.ActivityVo;
import com.zhhs.project.act.activity.domian.TableInfoVo;
import com.zhhs.project.act.activity.service.IActActivityBasicsService;
import com.zhhs.project.act.activity.service.IActActivityMarketService;
import com.zhhs.project.act.activity.service.IActActivityService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * 我的活动Controller
 *
 * @author zhhs
 * @date 2022-11-09
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wecom/activity/mineactivity")
@Slf4j
public class ActActivityController extends BaseController
{

    @Autowired
    private IActActivityService actActivityService;
    @Autowired
    private IActActivityBasicsService actActivityBasicsService;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private IActActivityMarketService actActivityMarketService;
    /**
     * 查询我的活动列表
     */
    @PreAuthorize("@ss.hasPermi('zhhs:actactivity:list')")
    @GetMapping("/list")
    public TableDataInfo list(ActActivity actActivity)
    {
        startPage();
        List<ActivityVo> list = actActivityService.selectActActivityVoList(actActivity);
        return getDataTable(list);
    }


    /**
     * 导出我的活动列表
     */
    @PreAuthorize("@ss.hasPermi('zhhs:actactivity:export')" )
    @Log(title = "我的活动" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(ActActivity actActivity)
    {
        List<ActActivity> list = actActivityService.selectActActivityList(actActivity);
        ExcelUtil<ActActivity> util = new ExcelUtil<ActActivity>(ActActivity.class);
        return util.exportExcel(list, "我的活动数据");
    }
    /**
     * 获取我的活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('zhhs:actactivity:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(actActivityService.selectActActivityById(id));
    }

//    /**
//     * 新增我的活动
//     */
//    @GetMapping("/createActivity")
//    public String add(@RequestParam("id") Long id, ModelMap mmap)
//    {
//        log.info("跳转进入我的活动新增界面,入参的活动id为==={}",id);
//        mmap.put("id",id);
//        return prefix + "/add";
//    }

    /**
     * 新增我的活动
     */
    @PreAuthorize("@ss.hasPermi('zhhs:actactivity:add')" )
    @Log(title = "我的活动" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addSave(ActActivity actActivity)
    {
        return toAjax(actActivityService.insertActActivity(actActivity));
    }

    /**
     * 新增保存我的活动引导模式
     * @return
     */

    @PostMapping("/addTableInfo")
    @ResponseBody
    public AjaxResult addTableInfo(@RequestBody TableInfoVo tableInfoVo, HttpServletRequest request) throws IOException, InvalidExtensionException {
        log.info("进入新增保存活动接口...............");
        log.info("入参内容====={}",tableInfoVo);
        return toAjax(actActivityService.insertTableInfoVo(tableInfoVo,request));
    }

    /**
     * 修改我的活动
     */
    @PreAuthorize("@ss.hasPermi('zhhs:actactivity:edit')" )
    @Log(title = "我的活动" , businessType = BusinessType.UPDATE)
    @PostMapping("/editTableInfo")
    public AjaxResult editTableInfo(@RequestBody TableInfoVo tableInfoVo){
        log.info("进入修改保存活动接口...............");
        log.info("入参内容====={}",tableInfoVo);
        return toAjax(actActivityService.updateTableInfoVo(tableInfoVo));
    }

//    /**
//     * 修改我的活动
//     */
//    @GetMapping("/edit/{id}")
//    public String edit(@PathVariable("id") Long id, ModelMap mmap)
//    {
//        ActActivity actActivity = actActivityService.selectActActivityById(id);
//        mmap.put("actActivity", actActivity);
//        return prefix + "/edit";
//    }

    /**
     * 修改我的活动
     */
    @PreAuthorize("@ss.hasPermi('zhhs:actactivity:edit')" )
    @Log(title = "我的活动" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult editSave(@RequestBody ActActivity actActivity)
    {
        return toAjax(actActivityService.updateActActivity(actActivity));
    }

    /**
     * 删除我的活动
     */
    @PreAuthorize("@ss.hasPermi('zhhs:actactivity:remove')" )
    @Log(title = "我的活动" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String ids)
    {
        return toAjax(actActivityService.deleteActActivityByIds(ids));
    }

    /**
     *复制操作
     * @param activityId
     * @return
     */
    @PostMapping("/copyActivity")
    @ResponseBody
    public AjaxResult copyActivity(Long activityId)
    {
        actActivityService.copyActivity(activityId);
        return AjaxResult.success();
    }

    /**
     * 修改状态
     * @param activityId
     * @param status
     * @return
     */
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(Long activityId, String status)
    {
        int i = actActivityService.changeStatus(activityId, status);
        return toAjax(i);
    }

    /**
     * 删除
     * @param activityId
     * @return
     */
    @PostMapping("/delAct")
    @ResponseBody
    public AjaxResult delAct(Long activityId)
    {
        Boolean aBoolean = actActivityService.delAct(activityId);
        return AjaxResult.success(aBoolean);
    }

    /**
     *查看二维码
     * @return
     */
    @GetMapping("/generateCode/{id}")
    public AjaxResult generateCode(@PathVariable Long id)
    {
        ActActivity actActivity = actActivityService.selectActActivityById(id);
        Long marketId = actActivity.getMarketId();
        ActActivityMarket actActivityMarket = actActivityMarketService.selectActActivityMarketById(marketId);
        String actUrl="";
        if ("1".equals(actActivityMarket.getActType())){
            actUrl=configService.selectConfigByKey("app.act.url");
        }else {
            actUrl=configService.selectConfigByKey("app.act.urlTwo");
        }
        actUrl=actUrl+id;
        actActivity.setUrlAct(actUrl);
        return AjaxResult.success(actActivity);
    }
}
